/**
 * 
 10:48:49
 */
package com.tagtraum.perf.gcviewer.imp;

import java.lang.reflect.Member;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;
import java.util.logging.Logger;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.tagtraum.perf.gcviewer.GCInfo;
import com.tagtraum.perf.gcviewer.model.AbstractGCEvent;
import com.tagtraum.perf.gcviewer.model.GCEvent;
import com.tagtraum.perf.gcviewer.model.GCModel;
import com.tagtraum.perf.gcviewer.model.GCinfoibm2_6;

/**
 * @author Administrator
 *
 */
public class IBMJ92_6Handler extends DefaultHandler {
	 private GCModel model;
	    private DateFormat cycleStartGCFormat5 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US);
	    private DateFormat cycleStartGCFormat6 = new SimpleDateFormat("MMM dd HH:mm:ss yyyy", Locale.US);
	    private DateFormat current = cycleStartGCFormat5;
	    protected AF_2_6 currentAF;
	    protected AF_2_6 tempcycle;
	    protected sys_2_6 currentSYS;
	    int currentTenured = 0; // 0 = none, 1=pre, 2=mid, 3=end
	    //added by wenguang.huang 2014��3��2�� ����3:36:37
        int currentmeminfo =0; //0 = none, 1=pre, 2=end
        int isgetGcPolicy =0;  //0 δ��ȡ, 1 �ѻ�ȡ
        String GcPolicy = "";
        gcinfo_summary gcinfosy;
        GCinfoibm2_6 gcInfo ;
        GCInfo gcinfo_lines;
        long usedheap =0;
        float maxms =0;
        float minms =0;
		//added end by wenguang.huang 2014��3��2�� ����3:36:37


	    private static Logger LOG = Logger.getLogger(IBMJ9SAXHandler.class.getName());
	    private Date begin = null;

	    public IBMJ92_6Handler(GCModel model) {
	        this.model = model;
	        gcInfo = new GCinfoibm2_6();
	        gcinfosy=new gcinfo_summary();//gcժҪ
	        gcinfosy.timestamp =new ArrayList();
	        gcinfosy.usedheapafter = new ArrayList();
	        gcinfosy.totalheapbefore = new ArrayList();
	        gcinfosy.usedheapbefore = new ArrayList();
	        gcinfosy.totalheapafter = new ArrayList();
	        gcinfosy.freeafterAt = new ArrayList();
	        gcinfosy.freebeforeAt = new ArrayList();
	        gcinfosy.requestAt= new ArrayList();
	        
	        gcinfosy.freedAt= new ArrayList();
	        gcinfosy.sinceAt= new ArrayList();
	        gcinfosy.afcompletedAt= new ArrayList();
	        gcinfosy.markAt= new ArrayList();
	        gcinfosy.sweepAt= new ArrayList();
	        gcinfosy.compactAt= new ArrayList();
	        gcinfosy.gccompletedAt= new ArrayList();
	        gcinfosy.OverheadAt= new ArrayList();
	        gcinfosy.requesttimeAt = new ArrayList();
	        
	        gcinfosy.marktimestamp =new ArrayList();	        
	        gcinfosy.sweeptimestamp =new ArrayList();	        
	        gcinfosy.compacttimestamp =new ArrayList();
	        
	    }

	    protected Date parseTime(String ts) throws ParseException {
	        try {
	            return current.parse(ts);
	        } 
	        catch (ParseException e) {
	            if (current != cycleStartGCFormat6) {

	                current = cycleStartGCFormat6;
	                return parseTime(ts);
	            }
	            throw e;
	        }
	    }

	    public void startElement(String namespaceURI, String sName, String qName,
	            Attributes attrs) throws SAXException {
	        // System.out.println("START: [" + qName + "]");
	    	
	    	if("initialized".equals(qName))
	    	{
	    	  gcinfosy.begintime =attrs.getValue("timestamp");
	    	}
			if(isgetGcPolicy == 0)
			{
				if("attribute".equals(qName)&&"gcPolicy".equals(attrs.getValue("name")))
				{
				  String Policy = attrs.getValue("value");
				  gcinfosy.gcPolicy = Policy.substring(Policy.indexOf(':')+1);
				  isgetGcPolicy = 1;
				}
			}
			
			if("exclusive-start".equals(qName))
			{
			   
			  
				
			}
			if("sys-start".equals(qName))
			{
				currentSYS =new sys_2_6(); 
			    String id = attrs.getValue("id");
			    //String ts = attrs.getValue("timestamp");
			    String intervalms = attrs.getValue("intervalms");
			    currentSYS.id = id;
			    currentSYS.afintervalms = Double.parseDouble(intervalms);   
				gcinfosy.forceCount++;
				
			}
			else if("concurrent-kickoff".equals(qName))
			{
				gcinfosy.concurrencyCount++;
			}
			if (currentAF == null) {

			    if ("af-start".equals(qName)) {
			        currentAF = new AF_2_6();
			        //String type = attrs.getValue("type");
			        String id = attrs.getValue("id");
			        String ts = attrs.getValue("timestamp");
			        try {
						final Date date = parseTime(ts);
						gcinfosy.requesttimeAt.add(date.getTime());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			        String intervalms = attrs.getValue("intervalms");
			        currentAF.id = id;
			        currentAF.afintervalms = Double.parseDouble(intervalms);
			        //currentAF.type = type;
			       
			        gcinfosy.AFCount ++;
			    	int  totalBytesRequested = Integer.valueOf(attrs.getValue("totalBytesRequested"));
			    	currentAF.totalBytesRequested = totalBytesRequested;
			    	gcinfosy.requestAt.add(totalBytesRequested);
			    	
			    	if(totalBytesRequested>gcinfosy.MaxBytesRequested)
			    		gcinfosy.MaxBytesRequested = totalBytesRequested;
			        //final Date date = parseTime(ts);
			        //currentAF.timestamp = date;
			        //if (begin == null) {
			        //    begin = date;
			        //    currentAF.elapsedTime = 0L;
			        //} 
			        //else {
			        //    currentAF.elapsedTime = (currentAF.timestamp.getTime() - begin
			        //            .getTime()) / 1000;
			        //    System.out.println("ElapsedTime: "
			        //            + currentAF.elapsedTime);
			        //}
			        // System.out.println("START: [af, " + type + "]");

			    }
			} 
			
			    //if ("time".equals(qName)) {
			        // String pauseStr = attrs.getValue("exclusiveaccessms");
			        // double pause = -1D;
			        // if(pauseStr != null){
			        // pause = Double.parseDouble(pauseStr);
			        // currentAF.totalTime = pause/1000;
			        // }

			        //String totalStr = attrs.getValue("totalms");
			        //double total = -1D;
			        //if (totalStr != null) {
			        //    total = Double.parseDouble(totalStr);
			        //    currentAF.totalTime = total / 1000;
			     //   }

			    //} 
				//added by wenguang.huang 2014��3��2�� ����12:36:05
			    if("cycle-start".equals(qName))
			    {
			       String timestamp = attrs.getValue("timestamp");
					   try {
						Date datetime = parseTime(timestamp);
						long time =datetime.getTime();
						gcinfosy.timestamp.add(time);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			    	String type = attrs.getValue("type");			    	
			    	double intervalms = Double.valueOf(attrs.getValue("intervalms"));
			    	tempcycle = new AF_2_6();
			    	if(currentAF!=null)
			    	{
			    	  currentAF.type = type;
			    	  currentAF.Intervalms = intervalms;
			    	}
			    	else if(currentSYS!=null)
			    	{
				      currentSYS.type = type;
				      currentSYS.Intervalms = intervalms;			    		
			    	}
			    	
			    	if("scavenge".equals(type))
			    	{
			    		gcinfosy.totalScavengeIntervalms = gcinfosy.totalScavengeIntervalms + intervalms; 			    	  
			    	}
			    	else 
			    	{
			    		gcinfosy.totalGlobalIntervalms = gcinfosy.totalGlobalIntervalms + intervalms;
			    	}

			    	
			    }
				//added end by wenguang.huang 2014��3��2�� ����12:36:05


			    else if ("gc-start".equals(qName)) {
			    	
			        String type = attrs.getValue("type");
			        
			        if(currentAF!=null)
			           currentAF.gcType = type;
			        else if(currentSYS!=null)
			           currentSYS.gcType =type;			        
			        if("scavenge".equals(type))
			        {
			        	gcinfosy.scavengeCount++;
			        }
			        else 
			        {
			        	gcinfosy.globalCount++;
					}
			    } 
			    else if ("timesms".equals(qName)) {
			        String markStr = attrs.getValue("mark");
			        double mark = -1D;
			        if (markStr != null) {
			            mark = Double.parseDouble(markStr);
			            currentAF.gcTimeMark = mark;
			        }
			        String sweepStr = attrs.getValue("sweep");
			        double sweep = -1D;
			        if (sweepStr != null) {
			            mark = Double.parseDouble(sweepStr);
			            currentAF.gcTimeSweep = sweep;
			        }
			    }
			    //added by wenguang.huang 2014��3��2�� ����12:50:13
			    else if ("mem-info".equals(qName))
			    {   
			    	currentmeminfo++;
			    	long free = Long.valueOf(attrs.getValue("free"));
			    	long total =Long.valueOf(attrs.getValue("total"));
			    	
			    	if(total>gcinfosy.maxtotalheap)
			    	  gcinfosy.maxtotalheap =total;
			    	if(total<gcinfosy.mintotalheap)
			          gcinfosy.mintotalheap = total;
			    	if(currentmeminfo%2==1)
			    	{
			    	  if(currentAF!=null)
			    	  {
			            currentAF.initialFreeBytes = free;
			            currentAF.initialTotalBytes = total;
			            
			    	  }
			    	  else if(currentSYS!=null)
			    	  {
			    		 currentSYS.initialFreeBytes = free;
			    		 currentSYS.initialTotalBytes = total;
					  }
			    		  
			    	  gcinfosy.gccount ++;
			    	  usedheap = total-free;
			    	  gcinfosy.usedheapbefore.add(total-free);
			    	  gcinfosy.totalheapbefore.add(total);
			    	  gcinfosy.freebeforeAt.add(free);
			    	}
			    	else if(currentmeminfo%2==0)
			    	{			    		
			    	  gcinfosy.usedheapafter.add(total-free);
			    	  gcinfosy.totalheapafter.add(total);
			    	  gcinfosy.freeafterAt.add(free);
			    	  gcinfosy.freedAt.add(usedheap-total+free);
			    	  if(currentAF!=null)
			    	  {
			            currentAF.afterFreeBytes = free;
			            currentAF.afterTotalBytes = total;
			    	  }
			    	  else if(currentSYS!=null)
			    	  {
			    		currentSYS.afterFreeBytes = free;
			    		currentSYS.afterTotalBytes = total; 
			    	  }
			    	}
			    	
			    if(tempcycle!=null)
			     {
			    	if(currentmeminfo%2==1)
			    	{
			    	  if(currentAF!=null)
			    	  {
			    		  tempcycle.initialFreeBytes = free;
			    		  tempcycle.initialTotalBytes = total;
			    	  }
			    	  else if(currentSYS!=null)
			    	  {
			    		 currentSYS.initialFreeBytes = free;
			    		 currentSYS.initialTotalBytes = total;
					  }
			    	}
			    	else if(currentmeminfo%2==0)
			    	{
			    	  if(currentAF!=null)
			    	  {
			    		  tempcycle.afterFreeBytes = free;
			    		  tempcycle.afterTotalBytes = total;
			    		  gcinfosy.totalglobalbytes = gcinfosy.totalglobalbytes+total-free;
			    		  
			    	  }
			    	  else if(currentSYS!=null)
			    	  {
			    		currentSYS.afterFreeBytes = free;
			    		currentSYS.afterTotalBytes = total; 
			    	  }
			    	}
			    }
			    	
			    }
			    
			    else if("mem".equals(qName))
			    {
			    	if("nursery".equals(attrs.getValue("type")))
			    	{
			    		if(currentmeminfo %2 == 1)
			    		{
			    	      long pre_nursery_free = Long.valueOf(attrs.getValue("free"));
			    	      long pre_nursery_total =Long.valueOf(attrs.getValue("total"));
			    	      int  pre_nursery_per =Integer.valueOf(attrs.getValue("percent"));
			    	      gcinfosy.totalnurseryper = gcinfosy.totalnurseryper + pre_nursery_per;
			    	      
			    		}
			    	   
			    	}
			    	else if("tenure".equals(attrs.getValue("type")))
			    	{
			    	  long pre_tenure_free = Long.valueOf(attrs.getValue("free"));
			          long pre_tenure_total =Long.valueOf(attrs.getValue("total"));
			          long pre_tenure_per = Long .valueOf(attrs.getValue("percent"));
			          if(currentmeminfo %2 == 1)
			          {
			        	  gcinfosy.totaltenureper = pre_tenure_per +gcinfosy.totaltenureper;
			          }
			    	}
			    	else if("soa".equals(attrs.getValue("type")))
			        {
			        	long soa_free = Long.valueOf(attrs.getValue("free"));
			        	long soa_total =Long.valueOf(attrs.getValue("total"));
			        	
			        	if(currentmeminfo %2 == 1)
			        	{
			        	  if(currentAF!=null)
			        	  {
			                currentAF.initialSOAFreeBytes = soa_free;
			                currentAF.initialSOATotalBytes = soa_total;
			        	  }
			        	  else if(currentSYS!=null)
			        	  {
				            currentSYS.initialSOAFreeBytes = soa_free;
				            currentSYS.initialSOATotalBytes = soa_total;
						  }
			        	}
			        	else if(currentmeminfo %2  == 0)
			        	{
			        	  if(currentAF!=null)
			        	  {
			                currentAF.afterSOAFreeBytes = soa_free;
			                currentAF.afterSOATotalBytes = soa_total;
			        	  }
			        	  else if(currentSYS!=null)
			        	  {
			                currentSYS.afterSOAFreeBytes = soa_free;
				            currentSYS.afterSOATotalBytes = soa_total;
						  }
			        		
			        	}
			        }
			    	else if("loa".equals(attrs.getValue("type")))
			        {
			        	long loa_free = Long.valueOf(attrs.getValue("free"));
			        	long loa_total =Long.valueOf(attrs.getValue("total"));
			        	
			        	if(currentmeminfo == 1)
			        	{
			        	  if(currentAF!=null)
			        	  {
			                 currentAF.initialLOAFreeBytes = loa_free;
			                 currentAF.initialLOATotalBytes = loa_total;
			        	  }
			        	  else if(currentSYS!=null)
			        	  {
			        	     currentSYS.initialLOAFreeBytes = loa_free;
				             currentSYS.initialLOATotalBytes = loa_total; 
			        		  
			        	  }
			        	}
			        	else if(currentmeminfo == 2)
			        	{
			        	  if(currentAF!=null)
			        	  {
			                currentAF.afterLOAFreeBytes = loa_free;
			                currentAF.afterLOATotalBytes = loa_total; 
			        	  }
			        	  else if(currentSYS!=null)
			        	  {
			        	    currentSYS.afterLOAFreeBytes = loa_free;
				            currentSYS.afterLOATotalBytes = loa_total;  
			        		  
			        	  }
			        	}
			         }
			    	
			    	
			    	
			    }
			    else if("remembered-set".equals(qName))
			    {
			    	
			    	int remembered_set_count = Integer.valueOf(attrs.getValue("count"));
			    	if(currentmeminfo == 1)
			    	{
			    	  if(currentAF!=null)
			    	   currentAF.preremembered_set_count = remembered_set_count;
			    	  else if(currentSYS!=null)
			    		  currentSYS.preremembered_set_count = remembered_set_count;
			    	}
			    	else if(currentmeminfo == 2)
			    	{
			    	  if(currentAF!=null)
			    		currentAF.aftremembered_set_count = remembered_set_count;
			    	  else if(currentSYS!=null)
			    		currentSYS.aftremembered_set_count = remembered_set_count;
			    	}
			    	
			    	
			    }   
			    else if("allocation-stats".equals(qName))
			    {
			    	int totalBytes = Integer.valueOf(attrs.getValue("totalBytes"));			    	
			    }
			    else if("gc-op".equals(qName))
			    {
			      if(currentAF!=null)
			    	currentAF.gcoptime = Double.valueOf(attrs.getValue("timems"));
			      else if(currentSYS!=null)
			    	currentSYS.gcoptime = Double.valueOf(attrs.getValue("timems"));
			      
			      long timeStamp =0;
			      float timems = Float.valueOf(attrs.getValue("timems"));
			      
			      if(maxms < timems)
			    	maxms = timems;
			      if(minms >timems)
			    	minms = timems;
			      
				   try {
					String timestamp = attrs.getValue("timestamp");
					Date datetime = parseTime(timestamp);
					timeStamp =datetime.getTime();
					
				       } catch (ParseException e) 
				       {
					    // TODO Auto-generated catch block
					    e.printStackTrace();
				       }
			    
			    if("mark".equals(attrs.getValue("type")))
			    {			    	
			        gcinfosy.markAt.add(timems);
                    gcinfosy.marktimestamp.add(timeStamp);
								        
			    }
			    
			    if("sweep".equals(attrs.getValue("type")))
			    {		
			    	gcinfosy.sweepAt.add(timems);	
				    gcinfosy.sweeptimestamp.add(timeStamp);
			
			    }
			    
			    if("compact".equals(attrs.getValue("type")))
			    {
			    	
			    	gcinfosy.compactAt.add(timems);			    	
			    	gcinfosy.compacttimestamp.add(timeStamp);

			    }
			    
			    
			    
			    }
			    else if("memory-copied".equals(qName))
			    {
			    	int bytes = Integer.valueOf(attrs.getValue("bytes"));
			    	int bytesdiscarded = Integer.valueOf(attrs.getValue("bytesdiscarded"));
			    	
			    	if("nursery".equals(attrs.getValue("type")))
			    	{
			    		gcinfosy.totalmcnurserybytes = gcinfosy.totalmcnurserybytes + bytes;
			    		gcinfosy.totalnurserydiscarded = gcinfosy.totalnurserydiscarded + bytesdiscarded;
			    	}
			    	else 
			    	{
			    		gcinfosy.totalmctenurebytes = gcinfosy.totalmctenurebytes + bytes;
			    		gcinfosy.totaltenurediscarded = gcinfosy.totalmctenurebytes + bytesdiscarded;
					}
			    }
			    else if("gc-end".equals(qName))
			    {
			    	Double durationms = Double.valueOf(attrs.getValue("durationms"));
			    	if(currentAF!=null)
			    	  currentAF.gcdurationms = durationms;
			    	else if(currentSYS!=null)
			    	{
			    	  currentSYS.gcdurationms = durationms;
			    	}
			    	
			    	if("scavenge".equals(attrs.getValue("type")))
			    	{			
			    		gcinfosy.scavengedurationms =durationms + gcinfosy.scavengedurationms;
			    	}
			    	else
			    	{
			    		gcinfosy.globaldurationms = durationms + gcinfosy.globaldurationms;
			    	
			    	}
			    	
			    }
			    
			    if("cycle_end".equals(qName))
			    {
			    	tempcycle =null;
			    }
				//added end by wenguang.huang 2014��3��2�� ����12:50:13


			    else if ("tenured".equals(qName)) {
			        currentTenured++;
			        String freeStr = attrs.getValue("freebytes");
			        long free = -1;
			        if (freeStr != null) {
			            free = Long.parseLong(freeStr);
			        }
			        String totalStr = attrs.getValue("totalbytes");
			        long total = -1;
			        if (totalStr != null) {
			            total = Long.parseLong(totalStr);
			        }

			        // For now only care about Total - don't break into SOA and
			        // LOA
			        if (currentTenured == 1) {
			            currentAF.initialFreeBytes = free;
			            currentAF.initialTotalBytes = total;
			            
			        } 
			        else if (currentTenured == 2) {
			            // ignore
			        }
			        else if (currentTenured == 3) {
			            currentAF.afterFreeBytes = free;
			            currentAF.afterTotalBytes = total;
			        } 
			        else {
			            LOG.warning("currentTenured is > 3!");
			        }
			    }
			    else if ("soa".equals(qName)) {
			        String freeStr = attrs.getValue("freebytes");
			        long free = -1;
			        if (freeStr != null) {
			            free = Long.parseLong(freeStr);
			        }
			        String totalStr = attrs.getValue("totalbytes");
			        long total = -1;
			        if (totalStr != null) {
			            total = Long.parseLong(totalStr);
			        }

			        if (currentTenured == 1) {
			            currentAF.initialSOAFreeBytes = free;
			            currentAF.initialSOATotalBytes = total;
			        }
			        else if (currentTenured == 2) {
			            // ignore
			        }
			        else if (currentTenured == 3) {
			            currentAF.afterSOAFreeBytes = free;
			            currentAF.afterSOATotalBytes = total;
			        }
			        else {
			            LOG.warning("currentTenured is > 3!");
			        }
			    } 
			    else if ("loa".equals(qName)) {
			        String freeStr = attrs.getValue("freebytes");
			        long free = -1;
			        if (freeStr != null) {
			            free = Long.parseLong(freeStr);
			        }
			        String totalStr = attrs.getValue("totalbytes");
			        long total = -1;
			        if (totalStr != null) {
			            total = Long.parseLong(totalStr);
			        }

			        if (currentTenured == 1) {
			            currentAF.initialLOAFreeBytes = free;
			            currentAF.initialLOATotalBytes = total;
			        } 
			        else if (currentTenured == 2) {
			            // ignore
			        } 
			        else if (currentTenured == 3) {
			            currentAF.afterLOAFreeBytes = free;
			            currentAF.afterLOATotalBytes = total;
			        } 
			        else {
			            LOG.warning("currentTenured is > 3!");
			        }
			    }
			    //added by wenguang.huang 2014��3��2�� ����12:19:52
			    else if("exclusive-end".equals(qName))
			    {
			    	String durationms =attrs.getValue("durationms");
			    	gcinfosy.endtime =attrs.getValue("timestamp");
			    	double elapsedTime = Double.parseDouble(durationms);
			    	if(currentAF!=null)
			    	  currentAF.elapsedTime =(long)elapsedTime;
			    	else if(currentSYS!=null)
			    	  currentSYS.elapsedTime =(long)elapsedTime;
			    	gcinfosy.elapsedTime = gcinfosy.elapsedTime +elapsedTime;
			           
			    	
			    }
				//added end by wenguang.huang 2014��3��2�� ����12:19:52


			
			
			
			
	    }

	    public void endElement(String namespaceURI, String simpleName,

	            String qualifiedName) throws SAXException {

	        if ("af-end".equals(qualifiedName)) {
	            System.out.println("In AF endElement!");
	            if (currentAF != null) {
	                GCEvent event = new GCEvent();
	                //if (!"tenured".equals(currentAF.type)) {
	                //    LOG.warning("Unhandled AF type: " + currentAF.type);
	                //}
	                //if (!"global".equals(currentAF.gcType)) {
	                //    LOG.warning("Different GC type: " + currentAF.gcType);
	                //} 
	                //else 
	                {
	                    event.setType(AbstractGCEvent.Type.Scavenge_GC);
	                }
	                if (currentAF.initialTotalBytes != -1
	                        && currentAF.initialFreeBytes != -1) {
	                    event.setPreUsed(currentAF.getPreUsedInKb());
	                }

	                if (currentAF.afterTotalBytes != -1
	                        && currentAF.afterFreeBytes != -1) {
	                    event.setPostUsed(currentAF.getPostUsedInKb());
	                }

	                if (currentAF.afterTotalBytes != -1) {
	                    event.setTotal(currentAF.getTotalInKb());
	                }

	                // event.setTimestamp(currentAF.timestamp.getTime());
	                event.setTimestamp(currentAF.elapsedTime);

	                if (currentAF.totalTime >= 0) {
	                    event.setPause(currentAF.totalTime);
	                }
	                if (currentAF.afterSOATotalBytes != -1
	                        && currentAF.afterSOAFreeBytes != -1
	                        && currentAF.initialSOAFreeBytes != -1
	                        && currentAF.initialSOATotalBytes != -1) {
	                    
	                    final GCEvent detailEvent = new GCEvent();
	                    detailEvent.setTimestamp(currentAF.elapsedTime);
	                    detailEvent.setType(AbstractGCEvent.Type.PS_YOUNG_GEN);
	                    detailEvent.setTenuredDetail(true);
	                    detailEvent.setPreUsed(currentAF.getPreUsedSoaInKb());
	                    detailEvent.setPostUsed(currentAF.getPostUsedSoaInKb());
	                    detailEvent.setTotal(currentAF.getTotalSoaInKb());
	                    event.add(detailEvent);
	                }

	                if (currentAF.afterLOATotalBytes != -1
	                        && currentAF.afterLOAFreeBytes != -1
	                        && currentAF.initialLOAFreeBytes != -1
	                        && currentAF.initialLOATotalBytes != -1) {
	                    
	                    final GCEvent detailEvent = new GCEvent();
	                    detailEvent.setTimestamp(currentAF.elapsedTime);
	                    detailEvent.setType(AbstractGCEvent.Type.PS_OLD_GEN);
	                    detailEvent.setTenuredDetail(true);
	                    detailEvent.setPreUsed(currentAF.getPreUsedLoaInKb());
	                    detailEvent.setPostUsed(currentAF.getPostUsedLoaInKb());
	                    detailEvent.setTotal(currentAF.getTotalLoaInKb());
	                    event.add(detailEvent);
	                }

	                model.add(event);
	                currentTenured = 0;
	                currentmeminfo = 0;
	                currentAF = null;
	            } 
	            else {
	                LOG.warning("Found end <af-end> tag with no begin tag");
	            }

	        }
	        else if("sys-end".equals(qualifiedName))
	        {
	        	//
	        	
	        }
	        if("verbosegc".equals(qualifiedName))
	        {
	        	//System.out.println("dddddddddddddddddd");
	        }
	    }
	    
	    //added by wenguang.huang 2014��3��3�� ����4:22:00

	    public void endDocument() throws SAXException {  
 
	        super.endDocument();  
	        
            //Scanner scanner = new Scanner(this.in, "UTF-8");
            //String text = scanner.useDelimiter("\\A").next();
            //model.setGccontent(text);
            //scanner.close();
            try {
				gcinfosy.runtime=parseTime(gcinfosy.endtime).getTime()-parseTime(gcinfosy.begintime).getTime();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            
            gcInfo.setPausetimescale((gcinfosy.elapsedTime*100) /gcinfosy.runtime );
            gcInfo.setRuntimescale(100- gcinfosy.elapsedTime*100/gcinfosy.runtime);
	        gcInfo.setAFCount(gcinfosy.AFCount);
	        gcInfo.setAstotalBytes(gcinfosy.astotalBytes);
	        gcInfo.setConcurrencyCount(gcinfosy.concurrencyCount);
	        gcInfo.setForceCount(gcinfosy.forceCount);
	        gcInfo.setGcPolicy(gcinfosy.gcPolicy);
	        gcInfo.setGlobalCount(gcinfosy.globalCount);
	        gcInfo.setGlobaldurationms(gcinfosy.globaldurationms);
	        gcInfo.setMaxBytesRequested(gcinfosy.MaxBytesRequested);
	        gcInfo.setScavengeCount(gcinfosy.scavengeCount);
	        gcInfo.setScavengedurationms(gcinfosy.scavengedurationms);
	        gcInfo.setTotalBytesRequested(gcinfosy.totalBytesRequested);
	        gcInfo.setTotalgcdurationms(gcinfosy.totalgcdurationms);
	        gcInfo.setTotalGlobalIntervalms(gcinfosy.totalGlobalIntervalms);
	        gcInfo.setTotaltenurediscarded(gcinfosy.totalnurserydiscarded);
	        gcInfo.setTotalScavengeIntervalms(gcinfosy.totalScavengeIntervalms);
	        gcInfo.setTotalmcnurserybytes(gcinfosy.totalmcnurserybytes/(1024*1024));
	        gcInfo.setTotalmctenurebytes(gcinfosy.totalmctenurebytes);
	        gcInfo.setTotalglobalbytes(gcinfosy.totalglobalbytes);
	        //double nurseryper = gcinfosy.totalnurseryper/gcinfosy.gccount;
	        //gcInfo.setNurseryper(0.1);
	        gcInfo.setNurseryper(gcinfosy.totalnurseryper/gcinfosy.gccount);
	        gcInfo.setTotaltenureper(gcinfosy.totaltenureper/gcinfosy.gccount);
	        gcInfo.setTimestamp(gcinfosy.timestamp);
	        gcInfo.setusedheapafter(gcinfosy.usedheapafter);
	        gcInfo.settotalheapbefore(gcinfosy.totalheapbefore);
	        gcInfo.setMaxtotalheap(gcinfosy.maxtotalheap);
	        gcInfo.setMintotalheap(gcinfosy.mintotalheap);
	        gcInfo.setFreeafterAt(gcinfosy.freeafterAt);
	        gcInfo.setFreebeforeAt(gcinfosy.freebeforeAt);
	        gcInfo.setTotalheapafter(gcinfosy.totalheapafter);
	        gcInfo.setUsedheapbefore(gcinfosy.usedheapbefore);
	        gcInfo.setFreedAt(gcinfosy.freedAt);
	        gcInfo.setRequestAt(gcinfosy.requestAt);
	        gcInfo.setRequesttimeAt(gcinfosy.requesttimeAt);
	        gcInfo.setMarkAt(gcinfosy.markAt);
	        gcInfo.setSweepAt(gcinfosy.sweepAt);
	        gcInfo.setCompactAt(gcinfosy.compactAt);
	        gcInfo.setSweeptimestamp(gcinfosy.sweeptimestamp);
	        gcInfo.setMarktimestamp(gcinfosy.marktimestamp);
	        gcInfo.setCompacttimestamp(gcinfosy.compacttimestamp);
	        gcInfo.setMaxms(maxms);
	        gcInfo.setMinms(minms);
	        model.Setgcinfo(gcInfo);
	        
	        
	    }  
		//added end by wenguang.huang 2014��3��3�� ����4:22:00



	}

	/**
	 * Holder of GC information for standard Allocation Failures
	 */
	class AF_2_6 {
	    String type;
	    String id;
	    Date timestamp;
	    long elapsedTime;
	    double afintervalms = -1;
	    long minRequestedBytes = -1;
	    double timeExclusiveAccessMs = -1;
	    long initialFreeBytes = -1;
	    long initialTotalBytes = -1;
	    long initialSOAFreeBytes = -1;
	    long initialSOATotalBytes = -1;
	    long initialLOAFreeBytes = -1;
	    long initialLOATotalBytes = -1;
	    long afterFreeBytes = -1;
	    long afterTotalBytes = -1;
	    long afterSOAFreeBytes = -1;
	    long afterSOATotalBytes = -1;
	    long afterLOAFreeBytes = -1;
	    long afterLOATotalBytes = -1;
	    String gcType;
	    double gcIntervalms = -1;
	    int gcSoftRefsCleared = -1;
	    int gcWeakRefsCleared = -1;
	    int gcPhantomRefsCleared = -1;
	    double gcTimeMark = -1;
	    double gcTimeSweep = -1;
	    double gcTimeCompact = -1;
	    double gcTime = -1;
	    double totalTime = -1;
        //added by wenguang.huang 2014��3��1�� ����10:33:32
	    int    MaxBytesRequested;
	    int    memory_copiedBytes;
	    int    meminfocount = 0;
	    String gcPolicy = "";
        double Intervalms = 0;
        double durationms = 0.0d;
        int    totalBytesRequested = 0;
	    double totalScavengeIntervalms = 0.0d;
	    double totalGlobalIntervalms = 0.0d;
	    double scavengedurationms = 0.0d;
	    double globaldurationms = 0.0d;
	    long   globalCount = 0;
	    long   scavengeCount = 0;
	    long   AFCount = 0;
	    long   forceCount = 0;
	    long   concurrencyCount = 0;
	    int    astotalBytes = 0;
	    double gcoptime = 0.0d;
	    long   mcnurserybytes =0;
	    long   mctenurebytes =0;
	    double gcdurationms =0;
	    int    preremembered_set_count =0;
	    int    aftremembered_set_count =0;
		//added end by wenguang.huang 2014��3��1�� ����10:33:32


	    public int getPreUsedInKb() {
	        return (int) ((initialTotalBytes - initialFreeBytes) / 1024);
	    }

	    public int getPostUsedInKb() {
	        return (int) ((afterTotalBytes - afterFreeBytes) / 1024);
	    }

	    public int getTotalInKb() {
	        return (int) (afterTotalBytes / 1024);
	    }

	    public int getPreUsedSoaInKb() {
	        return (int) ((initialSOATotalBytes - initialSOAFreeBytes) / 1024);
	    }

	    public int getPostUsedSoaInKb() {
	        return (int) ((afterSOATotalBytes - afterSOAFreeBytes) / 1024);
	    }

	    public int getTotalSoaInKb() {
	        return (int) (afterSOATotalBytes / 1024);
	    }

	    public int getPreUsedLoaInKb() {
	        return (int) ((initialLOATotalBytes - initialLOAFreeBytes) / 1024);
	    }

	    public int getPostUsedLoaInKb() {
	        return (int) ((afterLOATotalBytes - afterLOAFreeBytes) / 1024);
	    }

	    public int getTotalLoaInKb() {
	        return (int) (afterLOATotalBytes / 1024);
	    }
}
	
	class gcinfo_summary
	{
	    int    MaxBytesRequested;
	   
	    
	    String gcPolicy = "";
	    
	    int    totalBytesRequested = 0;
	    double totalScavengeIntervalms = 0.0d;
	    double totalGlobalIntervalms = 0.0d;
	    double scavengedurationms = 0.0d;
	    double globaldurationms = 0.0d;
	    long   globalCount = 0;
	    long   scavengeCount = 0;
	    long   AFCount = 0;
	    long   forceCount = 0;
	    long   concurrencyCount = 0;
	    int    astotalBytes = 0;		   
	    long   totalmcnurserybytes =0;
	    long   totalmctenurebytes =0;
	    double totalgcdurationms =0;
	    long   totalnurserydiscarded =0;
	    long   totaltenurediscarded =0;
	    long   totalglobalbytes =0;
		double elapsedTime =0;
		String begintime="";
		String endtime ="";
		long   runtime =0;
		double totalnurseryper = 0.0;//�������ǰ����nursery���ڴ�ռ��ƽ�����
		int    gccount = 0;//gcstart�Ĵ���
		double totaltenureper = 0.0;
		ArrayList timestamp ;//�洢ÿ��ͣ�ٵ�ʱ��
		ArrayList freebeforeAt;
		ArrayList freeafterAt;
		ArrayList usedheapbefore;
		ArrayList usedheapafter;
		ArrayList totalheapbefore;//�洢gcǰ�ܵĶѴ�С
		ArrayList totalheapafter;//�洢gcǰ�ܵĶѴ�С
		ArrayList requestAt;
		ArrayList freedAt;
		ArrayList sinceAt;
		ArrayList afcompletedAt;
		ArrayList markAt;
		ArrayList sweepAt;
		ArrayList compactAt;
		ArrayList gccompletedAt;
		ArrayList OverheadAt;
		ArrayList requesttimeAt;
		
		ArrayList marktimestamp;	        
		ArrayList sweeptimestamp;	        
		ArrayList compacttimestamp;
		
		long   maxtotalheap =0;
		long   mintotalheap =0;
		
	}

	/**
	 * Holder of GC information for standard Allocation Failures
	 */
	class sys_2_6 {
	    String type;
	    String id;
	    Date timestamp;
	    long elapsedTime;
	    double afintervalms = -1;
	    long minRequestedBytes = -1;
	    double timeExclusiveAccessMs = -1;
	    long initialFreeBytes = -1;
	    long initialTotalBytes = -1;
	    long initialSOAFreeBytes = -1;
	    long initialSOATotalBytes = -1;
	    long initialLOAFreeBytes = -1;
	    long initialLOATotalBytes = -1;
	    long afterFreeBytes = -1;
	    long afterTotalBytes = -1;
	    long afterSOAFreeBytes = -1;
	    long afterSOATotalBytes = -1;
	    long afterLOAFreeBytes = -1;
	    long afterLOATotalBytes = -1;
	    String gcType;
	    double gcIntervalms = -1;
	    int gcSoftRefsCleared = -1;
	    int gcWeakRefsCleared = -1;
	    int gcPhantomRefsCleared = -1;
	    double gcTimeMark = -1;
	    double gcTimeSweep = -1;
	    double gcTimeCompact = -1;
	    double gcTime = -1;
	    double totalTime = -1;
        //added by wenguang.huang 2014��3��1�� ����10:33:32
	    int    MaxBytesRequested;
	    int    memory_copiedBytes;
	    int    meminfocount = 0;
	    String gcPolicy = "";
        double Intervalms = 0;
        double durationms = 0.0d;
        int    totalBytesRequested = 0;
	    double totalScavengeIntervalms = 0.0d;
	    double totalGlobalIntervalms = 0.0d;
	    double scavengedurationms = 0.0d;
	    double globaldurationms = 0.0d;
	    long   globalCount = 0;
	    long   scavengeCount = 0;
	    long   AFCount = 0;
	    long   forceCount = 0;
	    long   concurrencyCount = 0;
	    int    astotalBytes = 0;
	    double gcoptime = 0.0d;
	    long   mcnurserybytes =0;
	    long   mctenurebytes =0;
	    double gcdurationms =0;
	    int    preremembered_set_count =0;
	    int    aftremembered_set_count =0;
		//added end by wenguang.huang 2014��3��1�� ����10:33:32


	    public int getPreUsedInKb() {
	        return (int) ((initialTotalBytes - initialFreeBytes) / 1024);
	    }

	    public int getPostUsedInKb() {
	        return (int) ((afterTotalBytes - afterFreeBytes) / 1024);
	    }

	    public int getTotalInKb() {
	        return (int) (afterTotalBytes / 1024);
	    }

	    public int getPreUsedSoaInKb() {
	        return (int) ((initialSOATotalBytes - initialSOAFreeBytes) / 1024);
	    }

	    public int getPostUsedSoaInKb() {
	        return (int) ((afterSOATotalBytes - afterSOAFreeBytes) / 1024);
	    }

	    public int getTotalSoaInKb() {
	        return (int) (afterSOATotalBytes / 1024);
	    }

	    public int getPreUsedLoaInKb() {
	        return (int) ((initialLOATotalBytes - initialLOAFreeBytes) / 1024);
	    }

	    public int getPostUsedLoaInKb() {
	        return (int) ((afterLOATotalBytes - afterLOAFreeBytes) / 1024);
	    }

	    public int getTotalLoaInKb() {
	        return (int) (afterLOATotalBytes / 1024);
	    }
}
